package com.gw.yunke.web;

import com.gw.yunke.domain.QAndA;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.dto.QAndAReq;
import com.gw.yunke.domain.dto.ResultData;
import com.gw.yunke.domain.dto.UserAnswerReq;
import com.gw.yunke.domain.dto.UserQAndAInfoResp;
import com.gw.yunke.service.IQAndAService;
import com.gw.yunke.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class QAndAController {

  @Autowired
  private IQAndAService qAndAService;
  @Autowired
  private IUserService userService;

  @RequestMapping(value = "/createQuestion",method = RequestMethod.POST)
  public ResultData createQuestion(@RequestBody QAndAReq qAndAReq,@CookieValue(value = "sso",required = false)String token) throws Exception {
    ResultData resultData = new ResultData();
    User user = new User();
    userService.checkLogin(token,user);
    if(!userService.checkCreatePermission(user,qAndAReq.getCourseId())){
      throw new Exception("您无权限提交新题目");
    }
    resultData.setCode(0);
    int result = qAndAService.createQuestion(qAndAReq);
    resultData.setCode(result > 0 ? 1:0);
    return resultData;
  }

  @RequestMapping(value = "/getQuestionSingleSelect",method = RequestMethod.POST)
  public ResultData getCourseQuestionsSingleSelect(@RequestBody QAndAReq qAndAReq, @CookieValue(value = "sso",required = false)String token) throws Exception {
    ResultData resultData = new ResultData();
    User user = new User();
    userService.checkLogin(token,user);

    List<QAndA> qAndAListSingleSelect = qAndAService.getQuestionSingleSelectByCourseId(qAndAReq.getCourseId());
    resultData.setCode(0);
    resultData.setData(qAndAListSingleSelect);

    return resultData;

  }

  @RequestMapping(value = "/answerTheQuestion",method = RequestMethod.POST)
  public ResultData answerTheQuestion(@RequestBody UserAnswerReq userAnswerReq,@CookieValue(value = "sso",required = false)String token) throws Exception {
    ResultData resultData = new ResultData();
    User user = new User();
    userService.checkLogin(token,user);
    resultData.setCode(0);
    userAnswerReq.setUserId(user.getId());
    int result = qAndAService.createAnswer(userAnswerReq);
    if(result > 0)
      resultData.setData(1);
    else
      resultData.setData(0);
    return resultData;
  }

  @RequestMapping(value = "/getAnswerInfo",method = RequestMethod.POST)
  public ResultData getUserAnswerResult(@RequestBody UserAnswerReq userAnswerReq,@CookieValue(required = false,value = "sso")String token) throws Exception {
    ResultData resultData = new ResultData();
    User user = new User();
    userService.checkLogin(token,user);
    resultData.setCode(0);

    List<UserQAndAInfoResp> userQAndAInfoRespList = qAndAService.getUserAnswerInfoByCourseId(userAnswerReq.getCourseId(),user.getId());
    resultData.setData(userQAndAInfoRespList);

    return resultData;
  }




}
